﻿using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace YueRu.Common.Xml
{
    /// <summary>
    /// XmlDocument扩展类
    /// 
    /// 目的:用于优化和减少代码书写量
    /// 备注:Element  译为:元素  
    ///      Document 译为:文档 
    ///      Node     译为:节点
    /// </summary>
    public class XmlDocumentExtender : XmlDocument
    {

        public XmlDocumentExtender(): base(){ }
      
        public XmlDocumentExtender(XmlNameTable table): base(new XmlImplementation(table)){ }

        /// <summary>
        /// 加载的文件名(含路径)
        /// </summary>
        /// <param name="filename"></param>
        public override void Load(string filename)
        {
            if (System.IO.File.Exists(filename))
            {
                base.Load(filename);
            }
            else
            {
                throw new Exception("文件: " + filename + " 不存在!");
            }
        }

        /// <summary>
        /// 在指定的Xml元素下,添加子Xml元素
        /// </summary>
        /// <param name="xmlElement">被追加子元素的Xml元素</param>
        /// <param name="childElementName">要添加的Xml元素名称</param>
        /// <param name="childElementValue">要添加的Xml元素值</param>
        /// <param name="isCDataSection">是否是CDataSection类型的子元素</param>
        /// <returns></returns>
        public bool AppendChildElementByNameValue(ref XmlElement xmlElement, string childElementName, object childElementValue, bool isCDataSection = false)
        {
            if ((xmlElement == null) || (xmlElement.OwnerDocument == null)) return false;

            //是否是CData类型Xml元素
            if (isCDataSection)
            {
                var tempdata = xmlElement.OwnerDocument.CreateCDataSection(childElementName);
                tempdata.InnerText = FiltrateControlCharacter(childElementValue.ToString());
                var childXmlElement = xmlElement.OwnerDocument.CreateElement(childElementName);
                childXmlElement.AppendChild(tempdata);
                xmlElement.AppendChild(childXmlElement);
            }
            else
            {
                var childXmlElement = xmlElement.OwnerDocument.CreateElement(childElementName);
                childXmlElement.InnerText = FiltrateControlCharacter(childElementValue.ToString());
                xmlElement.AppendChild(childXmlElement);
            }
            return true;
        }

        /// <summary>
        /// 在指定的Xml结点下,添加子Xml元素
        /// </summary>
        /// <param name="xmlNode">被追加子元素的Xml节点</param>
        /// <param name="childElementName">要添加的Xml元素名称</param>
        /// <param name="childElementValue">要添加的Xml元素值</param>
        /// <returns></returns>
        public bool AppendChildElementByNameValue(ref XmlNode xmlNode, string childElementName, object childElementValue)
        {
            return AppendChildElementByNameValue(ref xmlNode, childElementName, childElementValue, false);
        }


        /// <summary>
        /// 在指定的Xml结点下,添加子Xml元素
        /// </summary>
        /// <param name="xmlNode">被追加子元素的Xml节点</param>
        /// <param name="childElementName">要添加的Xml元素名称</param>
        /// <param name="childElementValue">要添加的Xml元素值</param>
        /// <param name="isCDataSection">是否是CDataSection类型的子元素</param>
        /// <returns></returns>
        public bool AppendChildElementByNameValue(ref XmlNode xmlNode, string childElementName, object childElementValue, bool isCDataSection)
        {
            if ((xmlNode == null) || (xmlNode.OwnerDocument == null)) return false;

            //是否是CData类型Xml结点
            if (isCDataSection)
            {
                var tempdata = xmlNode.OwnerDocument.CreateCDataSection(childElementName);
                tempdata.InnerText = FiltrateControlCharacter(childElementValue.ToString());
                var childXmlElement = xmlNode.OwnerDocument.CreateElement(childElementName);
                childXmlElement.AppendChild(tempdata);
                xmlNode.AppendChild(childXmlElement);
            }
            else
            {
                var childXmlElement = xmlNode.OwnerDocument.CreateElement(childElementName);
                childXmlElement.InnerText = FiltrateControlCharacter(childElementValue.ToString());
                xmlNode.AppendChild(childXmlElement);
            }
            return true;
        }

        /// <summary>
        /// 通过数据行向当前XML元素下追加子元素
        /// </summary>
        /// <param name="xmlElement">被追加子元素的Xml元素</param>
        /// <param name="columnCollection">当前数据表中的列集合</param>
        /// <param name="row">当前行数据</param>
        /// <param name="removecols">不会被追加的列名</param>
        /// <returns></returns>
        public bool AppendChildElementByDataRow(ref XmlElement xmlElement, DataColumnCollection columnCollection, DataRow row, string removecols = null)
        {
            if ((xmlElement == null) || (xmlElement.OwnerDocument == null)) return false;

            var columns = from DataColumn column in columnCollection
                          let isRemoveCols = (("," + removecols + ",").ToLower().IndexOf("," + column.Caption.ToLower() + ",", StringComparison.Ordinal) >= 0)
                          where (string.IsNullOrEmpty(removecols)) || !isRemoveCols
                          select column;

            foreach (var column in columns)
            {
                if (xmlElement.OwnerDocument == null) continue;

                var tempElement = xmlElement.OwnerDocument.CreateElement(column.Caption);
                tempElement.InnerText = FiltrateControlCharacter(row[column.Caption].ToString().Trim());
                xmlElement.AppendChild(tempElement);
            }

            return true;
        }

        /// <summary>
        /// 实始化节点, 如不存在则直接创建该结点, 当节点存在则清除当前路径下的所有子结点
        /// </summary>
        /// <param name="xmlpath"></param>
        /// <returns></returns>
        public XmlNode InitializeNode(string xmlpath)
        {
            return InitializeNode(xmlpath, true);
        }

        /// <summary>
        /// 实始化节点, 如不存在则直接创建该结点, 当节点存在则根据isClear判断是否要清除当前路径下的所有子结点
        /// </summary>
        /// <param name="xmlpath"></param>
        /// <param name="isClear"></param>
        /// <returns></returns>
        public XmlNode InitializeNode(string xmlpath, bool isClear)
        {
            var xmlNode = this.SelectSingleNode(xmlpath);
            if (xmlNode == null)
            {
                return CreateNode(xmlpath);
            }
            if (isClear)
            {
                xmlNode.RemoveAll();
            }
            return xmlNode;
        }

        /// <summary>
        /// 删除指定路径下面的所有子结点和自身
        /// </summary>
        /// <param name="xmlpath">指定路径</param>
        public void RemoveNodeAndChildNode(string xmlpath)
        {
            var xmlNodeList = this.SelectNodes(xmlpath);
            if (xmlNodeList == null || xmlNodeList.Count <= 0) return;
            foreach (XmlNode node in xmlNodeList)
            {
                node.RemoveAll();
                if (node.ParentNode != null)
                {
                    node.ParentNode.RemoveChild(node);
                }
            }
        }

        /// <summary>
        /// 创建指定路径下的节点
        /// </summary>
        /// <param name="xmlpath">节点路径</param>
        /// <returns></returns>
        public XmlNode CreateNode(string xmlpath)
        {
            var xpathArray = xmlpath.Split('/');
            var root = "";
            XmlNode parentNode = this;
            //建立相关节点
            for (var i = 1; i < xpathArray.Length; i++)
            {
                var node = this.SelectSingleNode(root + "/" + xpathArray[i]);
                // 如果当前路径不存在则建立,否则设置当前路径到它的子路径上
                if (node == null)
                {
                    var newElement = this.CreateElement(xpathArray[i]);
                    if (parentNode != null)
                    {
                        parentNode.AppendChild(newElement);
                    }
                }
                //设置低一级的路径
                root = root + "/" + xpathArray[i];
                parentNode = this.SelectSingleNode(root);
            }

            return parentNode;
        }

        /// <summary>
        /// 得到指定路径的节点值
        /// </summary>
        /// <param name="xmlnode">要查找节点</param>
        /// <param name="path">指定路径</param>
        /// <returns></returns>
        public string GetSingleNodeValue(XmlNode xmlnode, string path)
        {
            if (xmlnode == null)
            {
                return null;
            }

            if (xmlnode.SelectSingleNode(path) == null) return null;

            var selectSingleNode = xmlnode.SelectSingleNode(path);
            if (selectSingleNode != null)
            {
                return selectSingleNode.LastChild != null ? selectSingleNode.LastChild.Value : "";
            }
            return null;
        }

        /// <summary>
        /// 过滤控制字符,包括0x00 - 0x08,0x0b - 0x0c,0x0e - 0x1f
        /// </summary>
        /// <param name="content">要过滤的内容</param>
        /// <returns>过滤后的内容</returns>
        private static string FiltrateControlCharacter(string content)
        {
            return Regex.Replace(content, "[\x00-\x08|\x0b-\x0c|\x0e-\x1f]", "");
        }
    }
}
